using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.Department
{
    public partial class ICProductGroupsTreeListControl : THSTreeListControl
    {
        public override void InitializeControl()
        {
            base.InitializeControl();

            //Check edit category_Hieu.nguyen_29/04/2010
            DepartmentEntities entity = (DepartmentEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            if (entity.Module.HasPrivilege("EditCategory"))
            {
                foreach (DevExpress.XtraTreeList.Columns.TreeListColumn column in this.Columns)
                    column.OptionsColumn.AllowEdit = true;
            }
            this.ValidatingEditor +=new DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventHandler(ICProductGroupsTreeListControl_ValidatingEditor);
        }

        protected override void InitTreeListDataSource()
        {
            DepartmentEntities entity = (DepartmentEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            THSTreeList lst = new THSTreeList();
            if (this.THSDisplayRoot)
            {
                lst.Add(new ICProductGroupsInfo("Category"));
                lst[0].SubList = entity.ICProductGroupsList;
            }
            else
                lst = entity.ICProductGroupsList;
            this.DataSource = lst;
        }

        public void ICProductGroupsTreeListControl_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            if (e.Value.ToString() == String.Empty)
            {
                e.ErrorText = "Value can not be empty.";
                e.Valid = false;
                return;
            }

            DepartmentEntities entity = (DepartmentEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            THSTreeList lst = entity.ICProductGroupsList;
            DevExpress.XtraTreeList.Nodes.TreeListNode parentTreeListNode = this.FocusedNode.ParentNode;
            THSTreeListObject currentObject = lst.CurrentObject;
            THSTreeListObject parentNode = (THSTreeListObject)this.GetDataRecordByNode(parentTreeListNode);
            if (parentNode != null && parentNode.SubList.Count > 0)
            {
                foreach (THSTreeListObject objTreeListObject in parentNode.SubList)
                {
                    if ((objTreeListObject as ICProductGroupsInfo).ICProductGroupNo == e.Value.ToString() && currentObject != objTreeListObject)
                    {
                        e.ErrorText = "Category number can not be same as another.";
                        e.Valid = false;
                        return;
                    }
                }
            }
        }
    }
}
